package com.example.tool.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 描述:日期工具类
 *
 * @author: liangkun
 * @date: 2019/9/29 11:20
 */
public class CalendarUtil {
    public static final String DATE_FORMAT_ALL = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_SSS = "yyyy-MM-dd HH:mm:ss:SSS"; //毫秒级
    public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
    /**
     * 指定日期格式HH:mm
     */
    public static final String DATE_FORMAT_HM = "HH:mm";
    /**
     * @param
     * @return
     * @description 获取当前格式时间
     * @author liangkun
     * @time 2019/9/29 14:12
     */
    public static String getCurrentDate(String format) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * @param
     * @return
     * @description 获取年
     * @author liangkun
     * @time 2019/9/29 14:12
     */
    public static String getYearFromDate(String strDate) {
        Date date = getDate(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    /**
     * @param
     * @return
     * @description 获取月
     * @author liangkun
     * @time 2019/9/29 14:13
     */
    public static String getMonthFromDate(String strDate) {
        Date date = getDate(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        return String.valueOf(month);
    }

    /**
     * @param
     * @return
     * @description 获取日
     * @author liangkun
     * @time 2019/9/29 14:13
     */
    public static String getDayFromDate(String strDate) {
        Date date = getDate(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return String.valueOf(day);
    }

    /**
     * @param strDate 2018-05-10
     * @return Date格式
     */
    public static Date getDate(String strDate) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD);
        Date date = null;
        try {
            date = format.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将字符串型日期转换成日期
     *
     * @param dateStr    字符串型日期
     * @param dateFormat 日期格式
     * @return
     */
    public static Date stringToDate(String dateStr, String dateFormat) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * @param
     * @return
     * @description 根据日期获取年龄
     * @author liangkun
     * @time 2019/9/29 14:19
     */
    public static int getAge(String birthDay) {
        Date date = stringToDate(birthDay, DATE_FORMAT_YMD);
        Calendar cal = Calendar.getInstance();
        if (cal.before(date)) {
           return 0;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(date);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth){
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }

    /**
     * 获取时分
     *
     * @return 时分
     */
    public static String getHhMm() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_HM);
        Date date = new Date();

        return sdf.format(date);
    }


    /**
     * @return long
     * @description 获取当前时间
     * @author liangkun
     * @time 2019/12/6 10:04
     */
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }
}
